<script setup lang="ts">
// 接收父组件传递的参数
import { onMounted, reactive, ref } from 'vue'
import { listPictureTagCategoryUsingGet } from '@/api/fileController.ts'
import dayjs from 'dayjs'

interface Props {
  onsearch?: (searchParams: API.PictureQueryRequest) => void
}

const props = defineProps<Props>()

//存储分类
const categoryOptions = ref<string[]>([])
//存储标签
const tagOptions = ref<string[]>([])
//存储格式
const formatOptions = [
  { label: 'jpg', value: 'jpg' },
  { label: 'png', value: 'png' },
  { label: 'webp', value: 'webp' },
  { label: 'jpeg', value: 'jpeg' },
]

//存储搜索条件
const searchParams = reactive<API.PictureQueryRequest>({})
//日期范围
const dateRange = ref<[]>([])

//todo 进行搜索，将页面上录入的搜索条件，返回给父组件，由父组件进行真正地搜索操作
const doSearch = (searchParams: API.PictureQueryRequest) => {
  props.onsearch?.(searchParams)
}

/**
 * 调用后端获取图片标签的接口
 */
async function listPictureTagCategory() {
  const resp = await listPictureTagCategoryUsingGet()
  if (resp.data.data && resp.data.code === 0) {
    categoryOptions.value = (resp.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    tagOptions.value = (resp.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    console.log('调用后端获取图片标签的接口失败', resp.data.message)
  }
}

// 日期时间预设
const rangePresets = ref([
  { label: '最近7天', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: '最近14天', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: '最近30天', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: '最近90天', value: [dayjs().add(-90, 'd'), dayjs()] },
])

// 范围改变事件
const onRangeChange = (dates: any[], dateStrings: string[]) => {
  if (dates?.length >= 2) {
    searchParams.startEditTime = dates[0].toDate()
    searchParams.endEditTime = dates[1].toDate()
  } else {
    searchParams.startEditTime = undefined
    searchParams.endEditTime = undefined
  }
}

//重置
function doClear() {
  Object.keys(searchParams).forEach((key: any) => {
    searchParams[key] = undefined
  })
  dateRange.value = []
  props.onsearch?.(searchParams)
}

onMounted(() => {
  listPictureTagCategory()
})
</script>

<template>
  <!--  私有图库搜索框组件-->
  <div id="pictureSearchForm">
    <!-- 搜索框 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch" style="margin-bottom: 16px">
      <a-form-item label="关键词" name="searchText">
        <a-input v-model:value="searchParams.searchText" placeholder="请输入名称或简介" />
      </a-form-item>
      <!--      分类，用自动完成-->
      <a-form-item name="category" label="分类">
        <a-auto-complete
          v-model:value="searchParams.category"
          :options="categoryOptions"
          placeholder="请选择分类"
          allowClear
        />
      </a-form-item>
      <!--      标签，用选择框-->
      <a-form-item name="tags" label="标签">
        <a-select
          v-model:value="searchParams.tags"
          mode="tags"
          style="width: 96px"
          placeholder="请选择标签"
          :options="tagOptions"
          allowClear
        ></a-select>
      </a-form-item>
      <!--     日期时间范围查询-->
      <a-form-item name="dateRange" label="日期">
        <a-range-picker
          style="width: 320px"
          show-time
          format="YYYY/MM/DD HH:mm:ss"
          :placeholder="['编辑开始时间', '编辑结束时间']"
          v-model:value="dateRange"
          :presets="rangePresets"
          @change="onRangeChange"
        />
      </a-form-item>
      <!--      名称-->
      <a-form-item label="名称" name="name">
        <a-input v-model:value="searchParams.name" placeholder="请输入名称" />
      </a-form-item>
      <!--      简介-->
      <a-form-item label="简介" name="introduction">
        <a-input v-model:value="searchParams.introduction" placeholder="请输入简介" />
      </a-form-item>
      <!--      宽度-->
      <a-form-item label="宽度" name="picWidth">
        <a-input-number
          v-model:value="searchParams.picWidth"
          placeholder="请选择宽度"
          style="width: 96px"
        />
      </a-form-item>
      <!--      高度-->
      <a-form-item label="高度" name="picWidth">
        <a-input-number
          v-model:value="searchParams.picHeight"
          placeholder="请选择高度"
          style="width: 96px"
        />
      </a-form-item>
      <!--      格式-->
      <a-form-item name="picFormat" label="格式">
        <a-select
          v-model:value="searchParams.picFormat"
          mode="tags"
          style="width: 96px"
          placeholder="请选择格式"
          :options="formatOptions"
          allowClear
        ></a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="margin-left: 16px">搜索</a-button>
        <a-button html-type="reset" style="margin-left: 16px" @click="doClear">重置</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped>
#pictureSearchForm .ant-form-item {
  margin-top: 16px;
}
</style>
